<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\middleware\Auth;
use app\model\PostsModel;
use think\App;

class Posts extends Base {

    protected $middleware = [Auth::class];

    private $model;

    private $req;

    private $aid;

    private $page;

    private $limit;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->model = new PostsModel();

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 10;

        $this->aid = $this->isLogin();
    }



    public function getList(){

        $where=[];

        if(isset($this->req['post_status']) && $this->req['post_status'] != "" && in_array($this->req['post_status'],[-2,-1,0,1])){
            $where[] = ['post_status','=',$this->req['post_status']];
        }


        $list = $this->model->getList($where,$this->page,$this->limit,["update_at"=>"DESC"]);

        $total = $this->model->getTotal($where);

        return $this->succData($total,$list);

    }

    /*
     * 获取店铺列表
     * */
    public function getInfo(){

        if(!isset($this->req['post_id']) || empty($this->req['post_id'])){
            return $this->fail("帖子Id 不能为空");
        }

        $info = $this->model->getInfo($this->req['post_id']);

        return $this->succ($info);

    }



    /*
    * 新增/删除/修改
    * */
    public function updateStatus()
    {

        if(!isset($this->req['post_id']) || empty($this->req['post_id'])){
            return $this->fail("帖子Id 不能为空");
        }

        if(!isset($this->req['post_status']) || !in_array($this->req['post_status'],[1,-1,-2])){
            return $this->fail("请选择操作内容");
        }

        $res = $this->model->updateOne($this->req['post_id'],[
            "post_status"=>$this->req['post_status'],
        ]);

        if($res){

            $this->model->delCache($this->req['post_id']);

            return $this->succ();
        }else{
            return $this->fail();
        }
    }

}
